﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectFinal {
	public partial class frmAdmin : Form {
		public frmAdmin() {
			InitializeComponent();
			rb_allMedia.Checked = true;
			cb_adultBook.Checked = true;
			cb_childBook.Checked = true;
			cb_DVD.Checked = true;
			cb_video.Checked = true;
			fillPatronList();
			fillMediaList();
		}

		//Populatate DataGridView with Patron data
		private void fillPatronList() {
			dgv_patronList.DataSource = BusinessRules.getPatronList();
			this.dgv_patronList.Columns[0].Width = 35;  //ID
			this.dgv_patronList.Columns[1].Width = 171;	//Name
			this.dgv_patronList.Columns[2].Width = 35;	//Adult
			this.dgv_patronList.Columns[3].Width = 35;	//HasItems
			this.dgv_patronList.Columns[3].HeaderText = "Has Items";
		}//void fillPatronList()

		//Populatate DataGridView with Media data
		private void fillMediaList() {
			BindingList<Item> allMedia = BusinessRules.getItemList();
			BindingList<Item> displayedMedia = new BindingList<Item>();
			
			//Fill data based on filters
			foreach(Item i in allMedia) {
				if((rb_allMedia.Checked) ||																														//"All media" displays them all
					 (rb_available.Checked && i.isCheckedIn()) ||																				//"Available" is defined as media that isn't checked out
					 (rb_checkedOut.Checked && !i.isCheckedIn()) ||																			//"Checked out" is defined as...  checked out
					 (rb_overdue.Checked && !i.isCheckedIn() && i.Duedate < BusinessRules.getToday()))	//"Overdue" is defined as books due back before today
					if((i.getType() == MediaType.adultBook && cb_adultBook.Checked) ||								//Display Adult books if appropriate box is checked
							(i.getType() == MediaType.childBook && cb_childBook.Checked) ||								//Display Children's books if appropriate box is checked
							(i.getType() == MediaType.DVD && cb_DVD.Checked) ||														//Display DVDs if appropriate box is checked
							(i.getType() == MediaType.Video && cb_video.Checked))													//Display Videos if appropriate box is checked
						displayedMedia.Add(i);
			}
			dgv_mediaList.DataSource = displayedMedia;
			this.dgv_mediaList.Columns[0].Width = 45;		//ID
			this.dgv_mediaList.Columns[1].Width = 190;	//Title
			this.dgv_mediaList.Columns[2].Width = 55;		//Checked Out
			this.dgv_mediaList.Columns[2].HeaderText = "Checked Out";
			this.dgv_mediaList.Columns[3].Width = 80;		//Due Date
			this.dgv_mediaList.Columns[3].HeaderText = "Due Date";
			this.dgv_mediaList.Columns[4].Width = 70;		//PatronID
			this.dgv_mediaList.Columns[4].HeaderText = "Patron";
			this.dgv_mediaList.Columns[5].Width = 70;		//Media Type
			this.dgv_mediaList.Columns[5].HeaderText = "Media Type";
		}//void fillMediaList()

		private void btnMainMenu_Click(object sender, EventArgs e) {
			this.Close();
		}

		//Call this function when any of the Radio Buttons or Check Boxes is changed
		private void filters_CheckedChanged(object sender, EventArgs e) {
			fillMediaList();
		}
		
		private void btn_addPatron_Click(object sender, EventArgs e) {
			new frmPatron().ShowDialog();
			fillPatronList();
		}

		private void btn_removePatron_Click(object sender, EventArgs e) {
			if(dgv_patronList.SelectedRows.Count == 0) return;
			Patron temp = ((Patron)dgv_patronList.SelectedRows[0].DataBoundItem);
			if(temp == null) return;

			//A patron cannot be deleted if they have anything checked out
			if(temp.HasItems) {
				MessageBox.Show("A patron who has items checked out cannot be removed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			//Verify delete
			if(MessageBox.Show(this, "Are you sure you want to remove " + temp.Name + "?",
					"Verify Delete",
					MessageBoxButtons.YesNo,
					MessageBoxIcon.Question)
					 == DialogResult.No)
				return;
			
			//If we've made it this far, delete the selected Patron
			BusinessRules.removePatron(temp.ID);
			fillPatronList();
		}

		private void btn_addMedia_Click(object sender, EventArgs e) {
			new frmMedia().ShowDialog();
			fillMediaList();
		}

		private void btn_editMedia_Click(object sender, EventArgs e) {
			if(dgv_mediaList.SelectedRows.Count == 0) return;
			editMedia();
		}

		private void btn_removeMedia_Click(object sender, EventArgs e) {
			if(dgv_mediaList.SelectedRows.Count == 0) return;
			Item temp = ((Item)dgv_mediaList.SelectedRows[0].DataBoundItem);
			if(temp == null) return;

			//An Item cannot be deleted if it is checked out
			if(!temp.isCheckedIn()) {
				MessageBox.Show("Media that are checked out cannot be removed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			//Verify delete
			if(MessageBox.Show(this, "Are you sure you want to remove '" + temp.Title + "'?",
					"Verify Delete",
					MessageBoxButtons.YesNo,
					MessageBoxIcon.Question)
					 == DialogResult.No)
				return;

			//If we've made it this far, delete the selected Media
			BusinessRules.removeItem(temp.ID);
			fillMediaList();
		}

		private void dgv_mediaList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) {
			editMedia();
		}

		private void editMedia() {
			new frmMedia((Item)dgv_mediaList.SelectedRows[0].DataBoundItem).ShowDialog();
			fillMediaList();
		}

		private void dgv_patronList_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
			BusinessRules.sortPatrons();
			fillPatronList();
		}

		private void dgv_mediaList_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
			BusinessRules.sortItems();
			fillMediaList();
		}
	}
}
